<?php
namespace app\index\controller;

use app\index\model\KuaiDiOptionsModel;
use app\index\model\KuaiDiUserModel;

class User extends Base
{

    public function getUserInfo() {
        if(empty($this->userId)) {
            $res['success'] = false;
            $res['desc'] = '登录态失效';
            $res['errorcode'] = '200022';
            return json($res);
        }
        $model = new KuaiDiUserModel();
        $res = $model->getUserInfoById($this->userId);
        return json($res);
        
    }

    public function register()
    {
        $openId = input('openId', "");
        if(empty($openId)) {
            $res['success'] = false;
            $res['desc'] = 'msgId 参数错误';
            $res['errorcode'] = '200022';
            return json($res);
        }
        $userName = input('userName', '');
        $avatar = input('avatar', '');
        $tel = input('tel', '');


        $model = new KuaiDiUserModel();
        $data = [
            'tel' => $tel,
            'avatar' => $avatar,
            'user_name' => $userName,
            'open_id' => $openId,
        ];
        $res = $model->registerUser($data);
        return json($res);
    }

    public function upUserInfo() {
        $userName = input('userName', '');
        $avatar = input('avatar', '');
        $tel = input('tel', '');
        $userId = input('userId', 0);
        if(empty($userId)) {
            $res['success'] = false;
            $res['desc'] = 'msgId 参数错误';
            $res['errorcode'] = '200022';
            return json($res);
        }
        $model = new KuaiDiUserModel();
        
        $where['id'] = ['=', $userId];
        if($avatar) {
            $upData['avatar'] = $avatar;
        }
        if($userName) {
            $upData['user_name'] = $userName;
        }
        if($tel) {
            $upData['tel'] = $tel;
        }
        if($upData) {
            $res = $model->upUserInfo($upData, $where);
            return json($res);
        }
        $res['success'] = true;
        $res['desc'] = '';
        $res['errorcode'] = '';
        return json($res);
        
    }


}
